YAHOO.drafTrack.exampleEditorTree = function(id, oConfigs){
		 YAHOO.drafTrack.exampleEditorTree.superclass.constructor.call(this,id,oConfigs);
}

YAHOO.extend(YAHOO.drafTrack.exampleEditorTree, YAHOO.drafTrack.tableTree, {

//function to automatically load nodes based on json object
		loadNodes: function(nodesData,parentNode) {
		

		YAHOO.log('**loadNodes entered for ' + nodesData.name);
		YAHOO.log(nodesData);
		YAHOO.log(nodesData.length);
			if(!nodesData.length) {
				this.loadNode(nodesData,parentNode);
				return true;
 			}
			else {
				for(var i=0;i<nodesData.length;i++)
				{
					this.loadNode(nodesData[i],parentNode);
				}
				return true;
			}
		},
		
		loadNode: function(tempNode,parentNode, totalIter) {
				YAHOO.log('loadNode entered for '+tempNode.name);
				//We load the node
				var expanded = (tempNode.expanded)?tempNode.expanded:this.defaultExpansion;
				var addedNode = new YAHOO.drafTrack.exampleEditorNode(tempNode,parentNode,expanded);
				
				//If the min iteration is bigger than one, we need to add the node some more times
				
				YAHOO.log('nb totalIter for added node : ' + addedNode.totalIter);
				
				//If we provide the value for the total number of iteration, we set the node with it
				if(totalIter) {
					addedNode.totalIter = totalIter;
					addedNode.iter = totalIter - 1;
					YAHOO.log('iter value for this node: '+addedNode.iter);
				}
				
				if(addedNode.data.min >1 && addedNode.totalIter < addedNode.data.min) {
					YAHOO.log('min iteration addition entered');
					var newIteration = addedNode.data.clone(true);
					addedNode.changeTotalIter('add');
					this.loadNode(newIteration,parentNode,addedNode.totalIter);	
				}
				
				//If there are children, we call again the function with children data
				if(tempNode.child) this.loadNodes(tempNode.child,addedNode);
				else addedNode.isLeaf = true;
		
		},
		
		//Return a  JSON string that is the image of the tree
		//You can use then loadNodes to load them back
		getTreeJSONData: function() {
			var sb = [];
			for (var i = 0; i < this.getRoot().children.length; ++i) sb[sb.length] = this.getRoot().children[i].getNodeJSONData();
			return '{' + sb.join(",") + '}';
		},
});





YAHOO.drafTrack.exampleEditorNode=function(oData, oParent, expanded){
	YAHOO.drafTrack.exampleEditorNode.superclass.constructor.call(this,oData, oParent, expanded,true);
};
	
	
YAHOO.extend(YAHOO.drafTrack.exampleEditorNode, YAHOO.drafTrack.TableTreeNode, {
		
		totalIter:1, //total iteration of the particular node, checked when we want to add an element
		iter : 0, //iteration of the particular node, checked when we want to generate the output
		val: '', //contain the value for this iteration of the node
		// overrides YAHOO.widget.Node    	getNodeHtml: function() { 
    		
    	//Self call
    	oSelf = this;	
    		        var sb = [];		
		sb[sb.length] = '<div id="' + this.getDDId() + '" class="ygtvdd ';
		
		//If the parent is a root, we switch the color for the next node (children node as the same color than the parent)
		if(this.tree.odd && this.parent.isRoot()) {this.tree.odd = false;this.odd=false;}
		else if(!this.tree.odd && this.parent.isRoot()) {this.tree.odd = true;this.odd=true;}
		else this.odd = this.parent.odd;

		//We check what class to add		
		if(this.odd) sb[sb.length] = 'oddLine ';
		else sb[sb.length] = 'evenLine ';

	
		
		sb[sb.length] = '">';
		        sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';        sb[sb.length] = '<tr>';        
        if(this.tree.useDragHandle) sb[sb.length] = '<td id="' + this.getDDHandleId() + '"class="ygtvdhandle"></td>';
        
                for (var i=0;i<this.depth;++i) {            //sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '">&#160;</td>';            sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"><div class="ygtvspacer"></div></td>';        }
        
        if (this.hasIcon) {            sb[sb.length] = '<td';            sb[sb.length] = ' id="' + this.getToggleElId() + '"';            sb[sb.length] = ' class="' + this.getStyle() + '"';            sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '"';            if (this.hasChildren(true)) {                sb[sb.length] = ' onmouseover="this.className=';                sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';                sb[sb.length] = this.tree.id + '\',' + this.index +  ').getHoverStyle()"';                sb[sb.length] = ' onmouseout="this.className=';                sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';                sb[sb.length] = this.tree.id + '\',' + this.index +  ').getStyle()"';            }            //sb[sb.length] = '>&#160;</td>';            sb[sb.length] = '><div class="ygtvspacer"></div></td>';        }
        
        var col = this.tree.columns;
        
        
        //First column display the label        sb[sb.length] = '<td';        sb[sb.length] = ' id="' + this.getCellId(col[0].dataIndex) + '"';        sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
        //For the first column, we need to remove some width to match the depth
 		var firstColWidth = col[0].width - 18 * (this.depth + 1);
 		sb[sb.length] = ' width="' + firstColWidth +'"';		
 		sb[sb.length] = ' class="ygtvCell column_' + col[0].dataIndex + ' ' + this.getColumnClass(col[0].dataIndex) +' ';
        sb[sb.length] = '">';        sb[sb.length] = (this.data[col[0].dataIndex])?this.data[col[0].dataIndex]:'&nbsp;'; //sb[sb.length] = this.iter;
 //sb[sb.length] = ' + '+this.index       
        sb[sb.length] = '</td>';



		//Second column displays the editor
        sb[sb.length] = '<td';        sb[sb.length] = ' id="' + this.getCellId('editor') + '"';        sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
        sb[sb.length] = ' width="300px"'; 		
 		sb[sb.length] = ' class="ygtvCell column_value ';
 		//if(this.edited[col[i].dataIndex]) sb[sb.length] = 'edited ';
        sb[sb.length] = '">';        if(this.data.child) sb[sb.length] = '&nbsp;';
        else sb[sb.length] = this.getValueEditor(this.data.type);        sb[sb.length] = '</td>';
        
        
        
        
        
        
        //third column is for actions
        sb[sb.length] = '<td>';
        //first if the parameter is optional it can be removed
        if(this.data.mandatory == 'optional' ||  this.data.mandatory == 'conditional')  sb[sb.length] = '<button onclick="YAHOO.widget.TreeView.getTree(\''+ this.tree.id +'\').getNodeByIndex('+this.index+').removeNode()">Remove</button>';
         //second if the iteration max is not reached, another element can be added
//         YAHOO.log('max:' + this.data.max + ' current: ' + this.iter );
        if(this.data.max > 1 && this.totalIter <  this.data.max )  sb[sb.length] = '<button onclick="YAHOO.widget.TreeView.getTree(\''+ this.tree.id +'\').getNodeByIndex('+this.index+').addIteration()">Add one more iteration</button>';
        
        //third we check if we have more than the minimal amount of elements, in this case we can remove some of them
        if(this.totalIter > this.data.min && this.data.mandatory != 'optional' ) sb[sb.length] = '<button onclick="YAHOO.widget.TreeView.getTree(\''+ this.tree.id +'\').getNodeByIndex('+this.index+').removeNode()">Remove</button>'; 
        
        sb[sb.length] = '</td>';
        
        
        
        
        
                sb[sb.length] = '</tr>';        sb[sb.length] = '</table>';
        sb[sb.length] = '</div>';        return sb.join("");    },
    
    /** 	* Return the HTML string for the editor, this depends on the type of the output 	* 	* @method getValueEditor 	* @param dataIndex {String} value of the dataIndewx for which we should return the CSS style 	* @return {String} HTML string that should be set in the editor for updating the field 	*/
    getValueEditor: function(dataType) {
    if(dataType == 'boolean') {
    	if(!this.val) this.val =0;
    	var str = '<select onChange="YAHOO.widget.TreeView.getTree(\''+ this.tree.id +'\').getNodeByIndex('+this.index+').changeValue(this.value)">';
    	str += '<option value="0"';
    	if(this.val == 0) str += ' selected';
    	str += '>0</option><option value="1"';
    	if(this.val == 1) str+= ' selected';
    	str += '>1</option>';
    	return str;
    }
    else return '<input type="text" value="' + this.val + '" onKeyUp="YAHOO.widget.TreeView.getTree(\''+ this.tree.id +'\').getNodeByIndex('+this.index+').changeValue(this.value)"></input>';
    },
    
    /** 	* Update the value of the node 	* 	* @method changeValue 	* @param Value {String} New value that should be set for this Node 	* @return {boolean} 0 if failed, 1 if success 	*/
    changeValue: function(newValue) {
    	this.val = newValue;
    	return 1;
    },
    
    removeNode: function() {
    	//We save the tree for later use
       var treeSave = this.tree;
       
       //Need to lower the totalIter value
       this.changeTotalIter('remove');
       
	   treeSave.popNode(this);
	   
	   treeSave.draw();
    },
    
    addIteration: function() {
       YAHOO.log('adding an iteration');
       //We copy the data
       
       if(this.totalIter >=  this.data.max) return;
       
       var newIteration = this.data.clone(true);
       //We insert it in the tree
       var addedNode = new YAHOO.drafTrack.exampleEditorNode(newIteration,this,false);
       
       
       if(addedNode.data.child) this.tree.loadNodes(addedNode.data.child,addedNode);
       
       //We remove it from the tree
       this.tree.popNode(addedNode);
       
       //And we reintroduce it after the node
       addedNode.insertAfter(this);
       
       
       //We update the totalIter of all the nodes sharing the same name at the same level 
       this.changeTotalIter('add');
              
	   //and we can redraw the tree
	   this.tree.draw();
    },
    
    //calculate the number of iteration a node is displayed
      changeTotalIter: function(mode) {
     	
    	var nodes= this.parent.children;
    	
       	if(mode == 'add') var newtotalIter = this.totalIter + 1;
       	if(mode == 'remove') var newtotalIter = this.totalIter - 1;
       	var parentIndex = this.parent.index;
       	var u=0;
       	for(var i = 0;i<nodes.length;i++)
       	{
       		//Check that the node has the same name
       		if(nodes[i].data[this.tree.columns[0].dataIndex] == this.data[this.tree.columns[0].dataIndex]) {
       			
       			//As we can t popth node immediately in order to retrieve other iterations, we need to check that we don t add
       			// iterations to the node that will be popped just after
       			if(mode != 'remove' || (mode == 'remove' && nodes[i].index != this.index))
       			{
       				nodes[i].totalIter = newtotalIter;
       				nodes[i].iter=u;
       				u++;
       			}
       			//YAHOO.log(nodes[i].index + ' totalIter: ' + nodes[i].totalIter);	
       		}
       		
       	}
       	
       	//YAHOO.log('--------   ' + u + ' nodes found ---------');  

     },
     
     
  getNodeJSONData:function() {
        var col = this.tree.columns;
        var nodeData = null;
		var u=0;
        
        nodeData = YAHOO.lang.JSON.stringify(this.data[col[0].dataIndex]);
        u++;
        
        if(this.hasChildren()) {
        	var sb = [];
        	for (var i = 0; i < this.children.length; ++i)
        	{
        		sb[sb.length] = this.children[i].getNodeJSONData();
        	} 
        	if(!this.data.max || this.data.max == 1) return nodeData + ':{' + sb.join(",") + '}';
        	else if(this.iter == 0 && this.totalIter != 1 ) return nodeData + ':[{' + sb.join(",") + '}';
        	else if(this.iter == 0 && this.totalIter == 1 ) return nodeData + ':[{' + sb.join(",") + '}]';
        	else if(this.iter == this.totalIter - 1) return '{' + sb.join(",") + '}]';
        	else return '{' + sb.join(",") + '}';
        }
        else {    
        	if(this.data.max == 1) return nodeData + ':' +  YAHOO.lang.JSON.stringify(this.val);
        	else if(this.iter == 0 && this.totalIter != 1 ) return nodeData + ':[' + YAHOO.lang.JSON.stringify(this.val);
        	else if(this.iter == 0 && this.totalIter == 1 ) return nodeData + ':[' + YAHOO.lang.JSON.stringify(this.val) + ']';
        	else if(this.iter == this.totalIter - 1) return YAHOO.lang.JSON.stringify(this.val) + ']';
        	else return YAHOO.lang.JSON.stringify(this.val);
        }
  }
     
         
    
});